<?php

namespace app\admin\controller;

use app\admin\common\Base;
use think\Db;
use think\Session;
use PhpOffice\PhpSpreadsheet\Spreadsheet;

class Active extends Base
{
	// 菜单列表
	public function list()
	{
		$data = $this->request->param();
		if (isset($data['ajax']) && $data['ajax'] == 1) {
			$map = '';
			$page = isset($data['page']) ? $data['page'] : 1;
			$page = intval($page);
			$limit = isset($data['rows']) ? $data['rows'] : 10;
			$limit = intval($limit);
			$start = $limit * ($page - 1);


			if (isset($data['search'])) {
				if ($data['tj'] == 0) {
					//$list = Db::name('card')->where('tagslogs_sn', 'like', '%' . $data['search'] . '%')->order('id desc')->limit($start, $limit)->select();
					//$count = Db::name('card')->where('tagslogs_sn', 'like', '%' . $data['search'] . '%')->count();
                    $list =Db::name('card')->where('tagslogs_sn', 'like', '%' . $data['search'] . '%')->order('id desc')->select();
					$list=more_sn_one($list,'tagslogs_sn');
					$count=count($list);
					$list=array_slice($list,$start,$limit,false);//分页
				} else {
					$good_id = Db::name('good')->where('name', 'like', '%' . $data['search'] . '%')->select();
					$good_id = array_column($good_id, 'id');
					//$list = Db::name('card')->alias('a')->join('tagslogs b', 'a.tagslogs_sn = b.sn')->join('goodstags c', 'b.goodstags_id = c.id')->where('c.good_id', 'in', $good_id)->order('a.id desc')->limit($start, $limit)->select();
					//$count = Db::name('card')->alias('a')->join('tagslogs b', 'a.tagslogs_sn = b.sn')->join('goodstags c', 'b.goodstags_id = c.id')->where('c.good_id', 'in', $good_id)->order('a.id desc')->limit($start, $limit)->count();
                    $list = Db::name('card')->alias('a')->join('tagslogs b', 'a.tagslogs_sn = b.sn')->join('goodstags c', 'b.goodstags_id = c.id')->where('c.good_id', 'in', $good_id)->order('a.id desc')->select();
					$list=more_sn_one($list,'tagslogs_sn');
					$count=count($list);
					$list=array_slice($list,$start,$limit,false);//分页
				}
			} else {
				//$list = Db::name('card')->order('id desc')->limit($start, $limit)->select();
				//$count = Db::name('card')->count();
                $list =Db::name('card')->order('id desc')->select();
				$list=more_sn_one($list,'tagslogs_sn');
				$count=count($list);
				$list=array_slice($list,$start,$limit,false);//分页
			}

			foreach ($list as $key => $value) {
				// 查询标签
				$goodstags = Db::name('tagslogs')->where('sn', $value['tagslogs_sn'])->find();
				$good_id = Db::name('goodstags')->where('id', $goodstags['goodstags_id'])->value('good_id');
				$good = Db::name('good')->where('id', $good_id)->find();
				$list[$key]['good_name'] = $good['name'];
				$list[$key]['goodstags_sn'] = $goodstags['sn'];
				$list[$key]['good_model'] = $good['model'];
				$list[$key]['endtime'] = date('Y-m-d H:i:s', $value['endtime']);
				$list[$key]['starttime'] = date('Y-m-d H:i:s', $value['starttime']);
				// 添加时间
				$list[$key]['addtime'] = date('Y-m-d H:i:s', $value['addtime']);
				$list[$key]['baoxiutime'] = $list[$key]['starttime'] . ' 至 ' . $list[$key]['endtime'];
				$list[$key]['attr_sn'] = more_sn($goodstags['sn'])['sn'];
				switch ($value['checkstatus']) {
					case 0:
						$list[$key]['checkstatus_name'] = '待审核';
						break;
					case 1:
						$list[$key]['checkstatus_name'] = '通过';
						break;
					case 2:
						$list[$key]['checkstatus_name'] = '拒绝';
						break;
				}

				// 添加人
				$list[$key]['adduser'] = '';
				if(!empty($list[$key]['admin_id'])){
					$list[$key]['adduser'] = Db::name('admin')->where('id',$list[$key]['admin_id'])->value('realname');
				}
				// 审核人
				$list[$key]['checkuser'] = '';
				if(!empty($list[$key]['check_id'])){
					$list[$key]['checkuser'] = Db::name('admin')->where('id',$list[$key]['check_id'])->value('realname');
				}
				// 审核时间
				if(!empty($list[$key]['checktime'])){
					$list[$key]['checktime'] = date('Y-m-d H:i:s', $value['checktime']);
				}

				if($list[$key]['checktime'] == 0){
					$list[$key]['checktime'] = '';
				}

			}

			return $this->json_data($count, $list);
		}
		return $this->fetch();
	}
	// 激活统计
	public function desc()
	{
		// $descList = Db::name('card')->where('province', '<>', '')->select();
		// $list = [];
		// foreach ($descList as $key => $value) {
		// 	array_push($list, $value['province']);
		// }
		// $list = array_count_values($list);

		// $this->assign('list', $list);
		$info = [];
		$info['zl'] = Db::name('card')->where('checkstatus', 1)->count(); //总量
		$info['in_time'] = Db::name('card')->where('checkstatus', 1)->where('endtime', '>', time())->count(); //未过期
		$info['out_time'] = Db::name('card')->where('checkstatus', 1)->where('endtime', '<', time())->count(); //已过期
		$min_time = mktime(0,0,0,1,1,date('Y'));//本年初时间戳
		$where['addtime'] = ['>=', $min_time];
		$where['checkstatus'] = 1;
		$info['year_num'] = Db::name('card')->where($where)->count(); //年次数

		$min_time = mktime(0,0,0,date('m'),1,date('Y'));//本月初时间戳
		$where1['addtime'] = ['>=', $min_time];
		$where1['checkstatus'] = 1;
		$info['mon_num'] = Db::name('card')->where($where1)->count(); //月次数

		$min_time =  mktime(0,0,0,date('m'),date('d')-date('w')+1,date('y'));  //本周初时间戳
		$where2['addtime'] = ['>=', $min_time];
		$where2['checkstatus'] = 1;
		$info['week_num'] = Db::name('card')->where($where2)->count(); //周次数

		//取当前时间的前18天
		$today = date('Y-m-d');
		$date = [];
		$date_string = '';

		for ($i = 18; $i > 0; $i--) {
			$date[] = date("Y-m-d", strtotime("-{$i} day"));
			$date_string .= date("Y-m-d", strtotime("-{$i} day")) . ',';
		}
		$date[] = $today;
		$date_string .= $today;
		$info['date_string'] = $date_string;

		$login_sum = '';
		//$count=[];
		foreach ($date as $k => $val) {
			$min_time = strtotime($val);
			$max_time = $min_time + 60 * 60 * 24;
			$where['addtime'] = [['>=', $min_time], ['<=', $max_time]];
			$where['checkstatus'] = 1;
			$login_sum .= Db::name('card')->where($where)->count() . ',';
			//$c=Db::name('card')->where($where)->count();
		}
		$info['login_sum'] = $login_sum;
		//halt($info);
		$this->assign('info', $info);
		return $this->fetch();
	}


	public function active($id)
	{
		$info = Db::name('card')->where('id', $id)->find();
		if (!empty($info['starttime'])) {
			$info['starttime'] = date('Y-m-d H:i:s', $info['starttime']);
		}
		if (!empty($info['endtime'])) {
			$info['endtime'] = date('Y-m-d H:i:s', $info['endtime']);
		}

		// 查询出库日期
		$tagslogs = Db::name('tagslogs')->where('sn',$info['tagslogs_sn'])->find();
		$tagslogs['outtime'] = $tagslogs['outtime'] == 0?'':date('Y-m-d', $tagslogs['outtime']);

		$this->assign('info', $info);
		$this->assign('tagslogs', $tagslogs);
		return $this->fetch();
	}

	public function save()
	{
		$data = $this->request->param();
		$post = $data;
		unset($data['tagslogs_id']);
		unset($data['outtime']);
		unset($data['old_outtime']);
		if (empty($data['endtime']) || trim($data['endtime']) == '') {
			return ['status' => 0, 'msg' => '请输入保修期限'];
		}
		if ($data['starttime'] == $data['endtime']) {
			return ['status' => 0, 'msg' => '请输入保修期限'];
		}

		if (strtotime($data['starttime'] . ' 00:00:00') > strtotime($data['endtime'] . ' 23:59:59')) {
			return ['status' => 0, 'msg' => '保修开始日期不能大于截止日期'];
		}

		$data['starttime'] = $data['starttime'] . ' 00:00:00';
		$data['endtime'] = $data['endtime'] . ' 23:59:59';

		//新增绑定保修卡详情
		$pid=Db::name('card_record')->order('id desc')->value('id');
		if(empty($pid)){
			$pid=1;
		}

		$data['starttime'] = strtotime($data['starttime']);
		$data['endtime'] = strtotime($data['endtime']);
		$data['checkstatus'] = 1;
		$data['pid'] = $pid;
		$data['check_id'] = Session::get('user_id');
		$data['checktime'] = time();

		// 启动事务
		Db::startTrans();
		$res = Db::name('card')->where('id', $data['id'])->update($data);
		// 判断是否需要更新出库日期
		if($post['outtime'] != $post['old_outtime']){
			$outtime = strtotime($post['outtime']);
			$rest = Db::name('tagslogs')->where('id',$post['tagslogs_id'])->update(['outtime'=>$outtime]);
			if(!$rest){
				// 回滚事务
				Db::rollback();
				return ['status' => 0, 'msg' => "更新失败"];
			}
		}
		
		if ($res !== false) {
			$info = Db::name('card')->where('id', $data['id'])->find();
			$data['starttime'] = date('Y-m-d H:i:s', $data['starttime']);
			$data['endtime'] = date('Y-m-d H:i:s', $data['endtime']);
			//多序列审核
			$more_sn=more_sn($info['tagslogs_sn']);
			if($more_sn['status']==1){
				$more=$info;
				unset($more['id']);
				$cstr=checkstr($more_sn['sn'],',');//判断是否是多个序列码-是否存在英文逗号
				if($cstr){//包含--多个序列码-包含英文逗号
				    $sn_arr=strtoarr($more_sn['sn']);
					foreach ($sn_arr as $ksn => $vsn) {
						$more['tagslogs_sn']=$vsn;
				        Db::name('card')->insert($more);
					}
				}else{
					$more['tagslogs_sn']=$more_sn['sn'];
				    Db::name('card')->insert($more);
				}
				
			}

			  // 发送审核短信到用户
			  $msg_temp=get_msg_temp(3);
			  if($msg_temp['is_on']==1){
				  if($msg_temp['type']==1||$msg_temp['type']==2){//通知短信
					  $message = send_mobile_message($info['mobile_phone'], "jihuo",$info['tagslogs_sn']);
				  }
				  if($msg_temp['type']==1||$msg_temp['type']==3){//通知邮箱
					  if(!empty($info['email'])){
						  //sendToMail($data['email'], '【315云防伪】亲爱的用户，您有设备保修激活申请已提交', '【315云防伪】亲爱的用户，您有设备保修激活申请已提交，等待审核中.本邮件不用回复');
						  sendToMail($info['email'], $msg_temp['email_title'], $msg_temp['email_content'].'，保修期限为：'.$data['starttime'].'至'.$data['endtime'].'.本短信不用回复');
					  }

				  }
			  }

			  // 提交事务
				Db::commit();
				return ['status' => 1, 'msg' => '操作成功'];
			 
			//$message = send_mobile_message($info['mobile_phone'], "jihuo");
			// if ($message['status']) {
			// 	// 提交事务
			// 	Db::commit();
			// 	return ['status' => 1, 'msg' => '操作成功'];
			// } else {
			// 	// 回滚事务
			// 	Db::rollback();
			// 	return ['status' => 0, 'msg' => $message['msg']];
			// }
		} else {
			return ['status' => 0, 'msg' => '操作失败'];
		}
		//return $this->fetch();
	}

	// 审核
	public function add_status()
	{
		$data = $this->request->param();

			//新增绑定保修卡详情
		$pid=Db::name('card_record')->order('id desc')->value('id');
		if(empty($pid)){
			$pid=1;
		}
		$data['pid']=$pid;

		$res = Db::name('card')->where('id', $data['id'])->update([
			'checkstatus' => $data['checkstatus'],
			'check_id'=>Session::get('user_id'),
			'checktime'=>time(),
			'pid'=>$pid
		]);
		//写日志
		$temp = Db::name('card')->where('id', $data['id'])->find();
		if ($data['checkstatus'] == 1) {
			adminlog('保修卡：' . $temp['tagslogs_sn'], '审核通过');
		} else {
			// 发送审核短信到用户
			$msg_temp=get_msg_temp(4);
			if($msg_temp['is_on']==1){
				if($msg_temp['type']==1||$msg_temp['type']==2){//通知短信
					send_mobile_message($temp['mobile_phone'], "jujue",$temp['tagslogs_sn']);
				}
				if($msg_temp['type']==1||$msg_temp['type']==3){//通知邮箱
					if(!empty($temp['email'])){
						//sendToMail($data['email'], '【315云防伪】亲爱的用户，您有设备保修激活申请已提交', '【315云防伪】亲爱的用户，您有设备保修激活申请已提交，等待审核中.本邮件不用回复');
						sendToMail($temp['email'], $msg_temp['email_title'], $msg_temp['email_content']);
					}

				}
			}
			//send_mobile_message($temp['mobile_phone'], "jujue");
			adminlog('保修卡：' . $temp['tagslogs_sn'], '审核拒绝');
		}
		if ($res) {
			return ['status' => 1, 'msg' => '操作成功'];
		} else {
			return ['status' => 0, 'msg' => '操作失败'];
		}
	}

	// 待审核
	public function pre_list()
	{
		$data = $this->request->param();
		if (isset($data['ajax']) && $data['ajax'] == 1) {
			$map = '';
			$page = isset($data['page']) ? $data['page'] : 1;
			$page = intval($page);
			$limit = isset($data['rows']) ? $data['rows'] : 10;
			$limit = intval($limit);
			$start = $limit * ($page - 1);

			if (isset($data['search'])) {
				if ($data['tj'] == 0) {
					//$list = Db::name('card')->where('tagslogs_sn', 'like', '%' . $data['search'] . '%')->where('checkstatus', 0)->order('id desc')->limit($start, $limit)->select();
					//$count = Db::name('card')->where('tagslogs_sn', 'like', '%' . $data['search'] . '%')->where('checkstatus', 0)->count();
                    $list =Db::name('card')->where('tagslogs_sn', 'like', '%' . $data['search'] . '%')->where('checkstatus', 0)->order('id desc')->select();
					$list=more_sn_one($list,'tagslogs_sn');
					$count=count($list);
					$list=array_slice($list,$start,$limit,false);//分页
				} else {
					$good_id = Db::name('good')->where('name', 'like', '%' . $data['search'] . '%')->select();
					$good_id = array_column($good_id, 'id');
					//$list = Db::name('card')->alias('a')->join('tagslogs b', 'a.tagslogs_sn = b.sn')->join('goodstags c', 'b.goodstags_id = c.id')->where('c.good_id', 'in', $good_id)->where('a.checkstatus', 0)->order('a.id desc')->limit($start, $limit)->select();
					//$count = Db::name('card')->alias('a')->join('tagslogs b', 'a.tagslogs_sn = b.sn')->join('goodstags c', 'b.goodstags_id = c.id')->where('c.good_id', 'in', $good_id)->where('a.checkstatus', 0)->order('a.id desc')->limit($start, $limit)->count();
                    $list = Db::name('card')->alias('a')->join('tagslogs b', 'a.tagslogs_sn = b.sn')->join('goodstags c', 'b.goodstags_id = c.id')->where('c.good_id', 'in', $good_id)->where('a.checkstatus', 0)->order('a.id desc')->select();
					$list=more_sn_one($list,'tagslogs_sn');
					$count=count($list);
					$list=array_slice($list,$start,$limit,false);//分页
				}
			} else {
				//$list = Db::name('card')->where('checkstatus', 0)->order('id desc')->limit($start, $limit)->select();
				//$count = Db::name('card')->where('checkstatus', 0)->count();
                $list = Db::name('card')->where('checkstatus', 0)->order('id desc')->select();
				//$list=more_sn_one($list,'tagslogs_sn');防止次序列号不能显示
				$count=count($list);
				$list=array_slice($list,$start,$limit,false);//分页
			}

			foreach ($list as $key => $value) {
				// 查询标签
				$goodstags = Db::name('tagslogs')->where('sn', $value['tagslogs_sn'])->find();
				$good_id = Db::name('goodstags')->where('id', $goodstags['goodstags_id'])->value('good_id');
				$good = Db::name('good')->where('id', $good_id)->find();
				$list[$key]['good_name'] = $good['name'];
				$list[$key]['goodstags_sn'] = $goodstags['sn'];
				$list[$key]['good_model'] = $good['model'];
				$list[$key]['endtime'] = date('Y-m-d H:i:s', $value['endtime']);
				$list[$key]['starttime'] = date('Y-m-d H:i:s', $value['starttime']);
				$list[$key]['baoxiutime'] = $list[$key]['starttime'] . ' 至 ' . $list[$key]['endtime'];
				$list[$key]['attr_sn'] = more_sn($goodstags['sn'])['sn'];
				switch ($value['checkstatus']) {
					case 0:
						$list[$key]['checkstatus_name'] = '待审核';
						break;
					case 1:
						$list[$key]['checkstatus_name'] = '通过';
						break;
					case 2:
						$list[$key]['checkstatus_name'] = '拒绝';
						break;
				}

				$list[$key]['addtime'] = date('Y-m-d H:i:s', $value['addtime']);
				// 添加人
				$list[$key]['adduser'] = '';
				if(!empty($list[$key]['admin_id'])){
					$list[$key]['adduser'] = Db::name('admin')->where('id',$list[$key]['admin_id'])->value('realname');
				}
				// 审核人
				$list[$key]['checkuser'] = '';
				if(!empty($list[$key]['check_id'])){
					$list[$key]['checkuser'] = Db::name('admin')->where('id',$list[$key]['check_id'])->value('realname');
				}
				// 审核时间
				if(!empty($list[$key]['checktime'])){
					$list[$key]['checktime'] = date('Y-m-d H:i:s', $value['checktime']);
				}
				if($list[$key]['checktime'] == 0){
					$list[$key]['checktime'] = '';
				}
			}

			return $this->json_data($count, $list);
		}
		return $this->fetch();
	}

	// 已经审核
	public function done_list()
	{
		$data = $this->request->param();
		if (isset($data['ajax']) && $data['ajax'] == 1) {
			$map = '';
			$page = isset($data['page']) ? $data['page'] : 1;
			$page = intval($page);
			$limit = isset($data['rows']) ? $data['rows'] : 10;
			$limit = intval($limit);
			$start = $limit * ($page - 1);

			if (isset($data['search'])) {
				if ($data['tj'] == 0) {
					//$list = Db::name('card')->where('tagslogs_sn', 'like', '%' . $data['search'] . '%')->where('checkstatus', '<>', 0)->order('id desc')->limit($start, $limit)->select();
					//$count = Db::name('card')->where('tagslogs_sn', 'like', '%' . $data['search'] . '%')->where('checkstatus', '<>', 0)->count();
                    $list = Db::name('card')->where('tagslogs_sn', 'like', '%' . $data['search'] . '%')->where('checkstatus', '<>', 0)->order('id desc')->select();
				    $list=more_sn_one($list,'tagslogs_sn');
				    $count=count($list);
				    $list=array_slice($list,$start,$limit,false);//分页
				} else {
					$good_id = Db::name('good')->where('name', 'like', '%' . $data['search'] . '%')->select();
					$good_id = array_column($good_id, 'id');
					//$list = Db::name('card')->alias('a')->join('tagslogs b', 'a.tagslogs_sn = b.sn')->join('goodstags c', 'b.goodstags_id = c.id')->where('c.good_id', 'in', $good_id)->where('a.checkstatus', '<>', 0)->order('a.id desc')->limit($start, $limit)->select();
					//$count = Db::name('card')->alias('a')->join('tagslogs b', 'a.tagslogs_sn = b.sn')->join('goodstags c', 'b.goodstags_id = c.id')->where('c.good_id', 'in', $good_id)->where('a.checkstatus', '<>', 0)->order('a.id desc')->limit($start, $limit)->count();
                    $list = Db::name('card')->alias('a')->join('tagslogs b', 'a.tagslogs_sn = b.sn')->join('goodstags c', 'b.goodstags_id = c.id')->where('c.good_id', 'in', $good_id)->where('a.checkstatus', '<>', 0)->order('a.id desc')->select();
				    $list=more_sn_one($list,'tagslogs_sn');
				    $count=count($list);
				    $list=array_slice($list,$start,$limit,false);//分页
				}
			} else {
				//$list = Db::name('card')->where('checkstatus', '<>', 0)->order('id desc')->limit($start, $limit)->select();
				//$count = Db::name('card')->where('checkstatus', '<>', 0)->count();
                $list = Db::name('card')->where('checkstatus', '<>', 0)->order('id desc')->select();
				$list=more_sn_one($list,'tagslogs_sn');
				$count=count($list);
				$list=array_slice($list,$start,$limit,false);//分页
			}

			foreach ($list as $key => $value) {
				// 查询标签
				$goodstags = Db::name('tagslogs')->where('sn', $value['tagslogs_sn'])->find();
				$good_id = Db::name('goodstags')->where('id', $goodstags['goodstags_id'])->value('good_id');
				$good = Db::name('good')->where('id', $good_id)->find();
				$list[$key]['good_name'] = $good['name'];
				$list[$key]['goodstags_sn'] = $goodstags['sn'];
				$list[$key]['good_model'] = $good['model'];
				$list[$key]['endtime'] = date('Y-m-d H:i:s', $value['endtime']);
				$list[$key]['starttime'] = date('Y-m-d H:i:s', $value['starttime']);
				$list[$key]['baoxiutime'] = $list[$key]['starttime'] . ' 至 ' . $list[$key]['endtime'];
				$list[$key]['attr_sn'] = more_sn($goodstags['sn'])['sn'];
				switch ($value['checkstatus']) {
					case 0:
						$list[$key]['checkstatus_name'] = '待审核';
						break;
					case 1:
						$list[$key]['checkstatus_name'] = '通过';
						break;
					case 2:
						$list[$key]['checkstatus_name'] = '拒绝';
						break;
				}

				$list[$key]['addtime'] = date('Y-m-d H:i:s', $value['addtime']);
				// 添加人
				$list[$key]['adduser'] = '';
				if(!empty($list[$key]['admin_id'])){
					$list[$key]['adduser'] = Db::name('admin')->where('id',$list[$key]['admin_id'])->value('realname');
				}
				// 审核人
				$list[$key]['checkuser'] = '';
				if(!empty($list[$key]['check_id'])){
					$list[$key]['checkuser'] = Db::name('admin')->where('id',$list[$key]['check_id'])->value('realname');
				}
				// 审核时间
				if(!empty($list[$key]['checktime'])){
					$list[$key]['checktime'] = date('Y-m-d H:i:s', $value['checktime']);
				}

				if($list[$key]['checktime'] == 0){
					$list[$key]['checktime'] = '';
				}
			}

			return $this->json_data($count, $list);
		}
		return $this->fetch();
	}

	public function daochu()
	{
		$data = $this->request->param();
		if (!empty($data['stime'])) {
			$data['stime'] = strtotime($data['stime'] . " 00:00:00");
			$data['etime'] = strtotime($data['etime'] . " 23:59:59");
			switch ($data['status']) {
				case 0:
					$lists = Db::name('card')->order('id desc')->where('starttime', '>=', $data['stime'])->where('endtime', '<=', $data['etime'])->select();
					break;
				case 1:
					$lists = Db::name('card')->order('id desc')->where('starttime', '>=', $data['stime'])->where('endtime', '<=', $data['etime'])->where('checkstatus', 0)->select();
					break;
				case 2:
					$lists = Db::name('card')->order('id desc')->where('starttime', '>=', $data['stime'])->where('endtime', '<=', $data['etime'])->where('checkstatus', '<>', 0)->select();
					break;
				default:
					# code...
					break;
			}
		} else {
			switch ($data['status']) {
				case 0:
					$lists = Db::name('card')->order('id desc')->select();
					break;
				case 1:
					$lists = Db::name('card')->order('id desc')->where('checkstatus', 0)->select();
					break;
				case 2:
					$lists = Db::name('card')->order('id desc')->where('checkstatus', '<>', 0)->select();
					break;
				default:
					# code...
					break;
			}
		}

		foreach ($lists as $key => $value) {
			// 查询标签
			$goodstags = Db::name('tagslogs')->where('sn', $value['tagslogs_sn'])->find();
			$good_id = Db::name('goodstags')->where('id', $goodstags['goodstags_id'])->value('good_id');
			$good = Db::name('good')->where('id', $good_id)->find();
			$lists[$key]['good_name'] = $good['name'];
			$lists[$key]['goodstags_sn'] = $goodstags['sn'];
			$lists[$key]['good_model'] = $good['model'];
			$lists[$key]['endtime'] = $value['endtime'] == 0 ? '未指定' : date('Y-m-d H:i:s', $value['endtime']);
			$lists[$key]['starttime'] = $value['starttime'] == 0 ? '未指定' : date('Y-m-d H:i:s', $value['starttime']);
			$lists[$key]['status'] = '未过期';
			if (time() > $value['endtime']) {
				$list[$key]['status'] = '已过期';
			}

			switch ($value['checkstatus']) {
				case 0:
					$lists[$key]['checkstatus_name'] = '待审核';
					break;
				case 1:
					$lists[$key]['checkstatus_name'] = '通过';
					break;
				case 2:
					$lists[$key]['checkstatus_name'] = '拒绝';
					break;
			}
		}

		$spreadsheet = new Spreadsheet();
		$PHPSheet = $spreadsheet->getActiveSheet();

		$PHPSheet->setTitle("保修激活"); //给当前活动sheet设置名称
		$PHPSheet->setCellValue("A1", "商品名称")
			->setCellValue("B1", "序列号")
			->setCellValue("C1", "产品型号")
			->setCellValue("D1", "保修卡编号")
			->setCellValue("E1", "单位")
			->setCellValue("F1", "联系人")
			->setCellValue("G1", "电话")
			->setCellValue("H1", "手机")
			->setCellValue("I1", "电子邮件")
			->setCellValue("J1", "状态")
			->setCellValue("K1", "保修期限");

		$styleArray = [
			'alignment' => [
				'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER,
			],
		];
		foreach ($lists as $key => $val) {
			$spreadsheet->setActiveSheetIndex(0)
				->setCellValue("A" . ($key + 2), $val['good_name'])
				->setCellValue("B" . ($key + 2), $val['tagslogs_sn'])
				->setCellValue("C" . ($key + 2), $val['good_model'])
				->setCellValue("D" . ($key + 2), $val['bianhao'])
				->setCellValue("E" . ($key + 2), $val['danwei'])
				->setCellValue("F" . ($key + 2), $val['user'])
				->setCellValue("G" . ($key + 2), $val['phone'])
				->setCellValue("H" . ($key + 2), $val['mobile_phone'])
				->setCellValue("I" . ($key + 2), $val['email'])
				->setCellValue("J" . ($key + 2), $val['checkstatus_name'])
				->setCellValue("K" . ($key + 2), $val['starttime'] . '至' . $val['endtime']);

			$spreadsheet->setActiveSheetIndex(0)->getStyle('A' . ($key + 2))->applyFromArray($styleArray);
		}

		$spreadsheet->getActiveSheet()->getColumnDimension('F')->setWidth(30);

		$time = date('Y-m-d', time());
		// Redirect output to a client’s web browser (Xlsx)
		header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
		header('Content-Disposition: attachment;filename="保修激活' . $time . '.xlsx"');
		header('Cache-Control: max-age=0');
		// If you're serving to IE 9, then the following may be needed
		header('Cache-Control: max-age=1');
		// If you're serving to IE over SSL, then the following may be needed
		header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
		header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
		header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
		header('Pragma: public'); // HTTP/1.0

		$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');
		$writer->save('php://output');
	}
}
